package com.david.viewtest.view

import android.content.Context
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.LinearGradient
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.Shader
import android.graphics.Typeface
import android.text.Layout
import android.text.StaticLayout
import android.text.TextPaint
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import androidx.core.content.res.ResourcesCompat
import androidx.core.graphics.drawable.toBitmap
import com.david.viewtest.R

class CanvasTestView(
    context: Context,
    attrs: AttributeSet,
) : View(context, attrs) {
    val paint = TextPaint()


    val topBitmap: Bitmap = ResourcesCompat.getDrawable(resources, R.mipmap.canvas_img_top, null)!!.toBitmap()
    val bottomBitmap: Bitmap = ResourcesCompat.getDrawable(resources, R.mipmap.canvas_img_bottom, null)!!.toBitmap()
    val avatarBitmap: Bitmap = ResourcesCompat.getDrawable(resources, R.mipmap.canvas_img_avatar, null)!!.toBitmap()
    val miniprogramBitmap: Bitmap = ResourcesCompat.getDrawable(resources, R.mipmap.canvas_img_miniprogram, null)!!.toBitmap()
    val longImgBitmap: Bitmap = BitmapFactory.decodeStream(resources.assets.open("long_img3.png"))//ResourcesCompat.getDrawable(resources, R.drawable.long_img1, null)!!.toBitmap()


    val carIcon = ResourcesCompat.getDrawable(resources, R.mipmap.cavas_fr_img, null)!!.toBitmap()
    val medalicon = ResourcesCompat.getDrawable(resources, R.mipmap.canvas_medal, null)!!.toBitmap()
    val nameLabels = listOf(carIcon, medalicon)

    val titleBitmap: Bitmap = BitmapFactory.decodeStream(resources.assets.open("canvas_title3.png"))//ResourcesCompat.getDrawable(resources, R.drawable.long_img1, null)!!.toBitmap()

    //真实图片宽度/设计宽度
    val realDesignWidthRatio = longImgBitmap.width / 326f

    val desWidth = (realDesignWidthRatio * 390f).toInt()

    init {
        paint.textSize =  TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 13f , resources.displayMetrics)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        //super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        if (titleBitmap == null) {
            super.setMeasuredDimension(desWidth, ((longImgBitmap.height + 510 * realDesignWidthRatio).toInt()))
        } else {
            super.setMeasuredDimension(desWidth, ((longImgBitmap.height + 510 * realDesignWidthRatio + titleBitmap.height + 8 * realDesignWidthRatio).toInt()))
        }

    }

    override fun onDraw(canvas: Canvas) {
        //super.onDraw(canvas)
        paint.color = Color.parseColor("#FF5841")

        canvas.drawRect(Rect(0, 0, width, height), paint)
        paint.color = Color.WHITE
        canvas.drawRect(Rect((8 * realDesignWidthRatio).toInt(), (144 * realDesignWidthRatio).toInt(), (width - 8 * realDesignWidthRatio).toInt(), (height - 92 * realDesignWidthRatio).toInt()), paint)

        //绘制顶部默认图
        canvas.drawBitmap(
            topBitmap,
            Rect(0, 0, topBitmap.width, topBitmap.height),
            Rect(0, 0, desWidth, (topBitmap.height.toFloat() / topBitmap.width * desWidth).toInt()),
            paint
        )

        //绘制头像
        canvas.drawBitmap(
            avatarBitmap,
            Rect(0, 0, avatarBitmap.width, avatarBitmap.height),
            Rect((32 * realDesignWidthRatio).toInt(), (176 * realDesignWidthRatio).toInt(), (68 * realDesignWidthRatio).toInt(), (212 * realDesignWidthRatio).toInt()),
            paint
        )


        paint.color = Color.BLACK
        paint.typeface = Typeface.DEFAULT_BOLD

        canvas.drawText("极氪社区大管家", 80 * realDesignWidthRatio, 192 * realDesignWidthRatio, paint)
        val namewidth = paint.measureText("极氪社区大管家")
        paint.typeface = Typeface.DEFAULT


        paint.textSize =  TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 11f ,getResources().getDisplayMetrics())


        //绘制车标
        //canvas.drawBitmap(
        //    carIcon,
        //    Rect(0, 0, carIcon.width, carIcon.height),
        //    Rect((80 * realDesignWidthRatio + namewidth).toInt(), (181 * realDesignWidthRatio).toInt(), ((80 * realDesignWidthRatio + namewidth).toInt() + carIcon.width.toFloat() / carIcon.height * (12 * realDesignWidthRatio)).toInt(), (193 * realDesignWidthRatio).toInt()),
        //    paint
        //)

        var tempwidth = 4 * realDesignWidthRatio.toInt()
        nameLabels.forEach {
            //绘制昵称标签
            canvas.drawBitmap(
                it,
                Rect(0, 0, it.width, it.height),
                Rect((80 * realDesignWidthRatio + namewidth).toInt() + tempwidth, (181 * realDesignWidthRatio).toInt(), ((80 * realDesignWidthRatio + namewidth).toInt() + it.width.toFloat() / it.height * (12 * realDesignWidthRatio)).toInt() + tempwidth, (193 * realDesignWidthRatio).toInt()),
                paint
            )
            tempwidth += (it.width.toFloat() / it.height * (12 * realDesignWidthRatio)).toInt() + 4 * realDesignWidthRatio.toInt()
        }



        paint.color = Color.parseColor("#8E9496")
        canvas.drawText("打理社区大小事", 80 * realDesignWidthRatio, 210 * realDesignWidthRatio, paint)


        var titleYOffset = 0
        if (titleBitmap != null) {
            titleYOffset = (8 * realDesignWidthRatio + titleBitmap.height).toInt()

            //绘制标题
            canvas.drawBitmap(
                titleBitmap,
                Rect(0, 0, titleBitmap.width, titleBitmap.height),
                Rect((32 * realDesignWidthRatio).toInt(), (218 * realDesignWidthRatio).toInt(), (width - 32 * realDesignWidthRatio).toInt(), (218 * realDesignWidthRatio).toInt() + titleBitmap.height),
                paint
            )
        }




        //绘制内容图
        canvas.drawBitmap(
            longImgBitmap,
            Rect(0, dp2px(18f).toInt(), longImgBitmap.width, longImgBitmap.height),
            Rect((32 * realDesignWidthRatio).toInt(),titleYOffset +  (218 * realDesignWidthRatio).toInt(), (width - 32 * realDesignWidthRatio).toInt(), titleYOffset + (218 * realDesignWidthRatio).toInt() + longImgBitmap.height),
            paint
        )

        paint.setShader(LinearGradient(0f, (height - 350 * realDesignWidthRatio), 0f, height - 292 * realDesignWidthRatio, Color.TRANSPARENT, Color.WHITE, Shader.TileMode.CLAMP))

        canvas.drawRect(Rect((32 * realDesignWidthRatio).toInt(), (height - 350 * realDesignWidthRatio).toInt(), (width - 32 * realDesignWidthRatio).toInt(), (height - 292 * realDesignWidthRatio).toInt()), paint)
        paint.setShader(null)



        paint.color = Color.parseColor("#64696B")
        paint.textSize =  TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 14f ,getResources().getDisplayMetrics())
        canvas.drawText("#极氪001提车日记 #极氪生活", 32 * realDesignWidthRatio, height - 250 * realDesignWidthRatio, paint)

        paint.color = Color.parseColor("#8E9496")
        paint.textSize =  TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12f ,getResources().getDisplayMetrics())
        canvas.drawText("2024/05/22", 32 * realDesignWidthRatio, height - 222 * realDesignWidthRatio, paint)


        //绘制底部线
        canvas.drawRect(Rect((32 * realDesignWidthRatio).toInt(), (height - 188 * realDesignWidthRatio).toInt(), (width - 32 * realDesignWidthRatio).toInt(), (height - 187.5 * realDesignWidthRatio).toInt()), paint)


        paint.color = Color.parseColor("#64696B")
        paint.textSize =  TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 13f ,getResources().getDisplayMetrics())
        canvas.drawText("微信内长按二维码", 32 * realDesignWidthRatio, height - 146 * realDesignWidthRatio, paint)
        canvas.drawText("去「极氪汽车」小程序查看完整内容", 32 * realDesignWidthRatio, height - 126 * realDesignWidthRatio, paint)


        //绘制小程序图片
        canvas.drawBitmap(
            miniprogramBitmap,
            Rect(0, 0, miniprogramBitmap.width, miniprogramBitmap.height),
            Rect((286 * realDesignWidthRatio).toInt(), (height - 176 * realDesignWidthRatio).toInt(), (width - 32 * realDesignWidthRatio).toInt(), (height - 104 * realDesignWidthRatio).toInt()),
            paint
        )


        //绘制底部默认图
        canvas.drawBitmap(bottomBitmap, Rect(0, 0, bottomBitmap.width, bottomBitmap.height), Rect(0, (height - 92 * realDesignWidthRatio).toInt(), width, height), paint)



    }

    fun dp2px(dp : Float): Float{
        val r = Resources.getSystem()
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.displayMetrics)
    }
}